package com.tim.app.ui.popup;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.tim.app.R;
import com.tim.app.RT;
import com.tim.app.ui.adapter.ReportAdapter;

import org.json.JSONObject;


/**
 * 举报弹窗
 */
public class ReportPop extends PopupWindow implements ReportAdapter.onReportListener, View.OnClickListener {

    private Context mContext;
    private ListView lv_report;
    private Button btn_cancel;
    private ReportAdapter reportAdapter;
    public String uid, tid;
    public int type;
    private String[] reportArray;

    public ReportPop(Context context, String uid, String tid, int type) {
        this.mContext = context;
        this.uid = uid;
        this.tid = tid;
        this.type = type;
        setWidth(RT.getScreenWidth() * 10 / 13);
        setHeight(-2);
        setFocusable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable());
        setAnimationStyle(R.style.ReportPopAnim);
        View view = LayoutInflater.from(context).inflate(R.layout.popup_report, null);
        setContentView(view);
        initView(view);
        initData();
    }

    private void initView(View view) {
        lv_report = (ListView) view.findViewById(R.id.lv_report);
        btn_cancel = (Button) view.findViewById(R.id.btn_cancel);

        btn_cancel.setOnClickListener(this);
    }

    private void initData() {
        reportArray = mContext.getResources().getStringArray(R.array.reportArray);
        reportAdapter = new ReportAdapter(reportArray);
        reportAdapter.setOnReportListener(this);
        lv_report.setAdapter(reportAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_cancel:
                dismiss();
                break;
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        backgroundAlpha(1f);
    }

    public void show(View parent) {
        if (!isShowing()) {
            this.showAtLocation(parent, Gravity.CENTER, 0, 0);
            backgroundAlpha(0.5f);
        }
    }

    @Override
    public void onReport(int position) {
        addReport(position);
        dismiss();
    }

    public void addReport(int position) {
//        API_Live.ins().addReport(AppKey.HTTP_TAG, UserManager.ins().getUserId(), uid, reportArray[position], roomid, type, new JsonResponseCallback() {
//            @Override
//            public boolean onJsonResponse(JSONObject json, int errCode, String errMsg, int id, boolean fromCache) {
//                if (errCode == 200) {
//                    ToastUtil.showToast(RT.getString(R.string.report_success));
//                }
//                return false;
//            }
//        });
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        ((Activity) mContext).getWindow().setAttributes(lp);
    }
}
